<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Creates or open a shared memory segment</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.shm-attach" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">shm_attach</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">shm_attach</span> &mdash; <span class="dc-title">Creates or open a shared memory segment</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.shm-attach-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>shm_attach</strong></span>
    ( <span class="methodparam"><span class="type">int</span> <code class="parameter">$key</code></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$memsize</code></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$perm</code><span class="initializer"> = 0666</span></span>
  ]] ) : <span class="type">resource</span></div>

  <p class="para rdfs-comment">
   <span class="function"><strong>shm_attach()</strong></span> returns an id that can be used to access
   the System V shared memory with the given <code class="parameter">key</code>, the
   first call creates the shared memory segment with 
   <code class="parameter">memsize</code> and the optional perm-bits 
   <code class="parameter">perm</code>.
  </p>
  <p class="para">
   A second call to <span class="function"><strong>shm_attach()</strong></span> for the same
   <code class="parameter">key</code> will return a different shared memory
   identifier, but both identifiers access the same underlying
   shared memory. <code class="parameter">memsize</code> and
   <code class="parameter">perm</code> will be ignored.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.shm-attach-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">key</code></dt>

     <dd>

      <p class="para">
       A numeric shared memory segment ID
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">memsize</code></dt>

     <dd>

      <p class="para">
       The memory size. If not provided, default to the
       <em>sysvshm.init_mem</em> in the <var class="filename">php.ini</var>, otherwise 10000
       bytes.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">perm</code></dt>

     <dd>

      <p class="para">
       The optional permission bits. Default to 0666.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.shm-attach-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   Returns a shared memory segment identifier.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.shm-attach-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>5.3.0</td>
       <td>
         This function now returns a <a href="language.types.resource.html" class="link">资源(resource)</a> instead of an 
         <a href="language.types.integer.html" class="link">integer</a>.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.shm-attach-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    This function used to return an integer value prior to 
    PHP 5.3.0. To achieve the same value in a portable manner, the 
    return value can be cast to an integer like:
   </p>
   <p class="para">
    <div class="example" id="example-4453">
     <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;Create&nbsp;a&nbsp;temporary&nbsp;file&nbsp;and&nbsp;return&nbsp;its&nbsp;path<br /></span><span style="color: #0000BB">$tmp&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">tempnam</span><span style="color: #007700">(</span><span style="color: #DD0000">'/tmp'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'PHP'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;Get&nbsp;the&nbsp;file&nbsp;token&nbsp;key<br /></span><span style="color: #0000BB">$key&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">ftok</span><span style="color: #007700">(</span><span style="color: #0000BB">$tmp</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'a'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;Attach&nbsp;the&nbsp;SHM&nbsp;resource,&nbsp;notice&nbsp;the&nbsp;cast&nbsp;afterwards<br /></span><span style="color: #0000BB">$id&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">shm_attach</span><span style="color: #007700">(</span><span style="color: #0000BB">$key</span><span style="color: #007700">);<br /><br />if&nbsp;(</span><span style="color: #0000BB">$id&nbsp;</span><span style="color: #007700">===&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;die(</span><span style="color: #DD0000">'Unable&nbsp;to&nbsp;create&nbsp;the&nbsp;shared&nbsp;memory&nbsp;segment'</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;Cast&nbsp;to&nbsp;integer,&nbsp;since&nbsp;prior&nbsp;to&nbsp;PHP&nbsp;5.3.0&nbsp;the&nbsp;resource&nbsp;id&nbsp;<br />//&nbsp;is&nbsp;returned&nbsp;which&nbsp;can&nbsp;be&nbsp;exposed&nbsp;when&nbsp;casting&nbsp;a&nbsp;resource<br />//&nbsp;to&nbsp;an&nbsp;integer<br /></span><span style="color: #0000BB">$id&nbsp;</span><span style="color: #007700">=&nbsp;(integer)&nbsp;</span><span style="color: #0000BB">$id</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
     </div>

    </div>
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.shm-attach-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="shm_detach.html" class="function" rel="rdfs-seeAlso">shm_detach()</a> - Disconnects from shared memory segment</span></li>
    <li class="member"><span class="function"><a href="ftok.html" class="function" rel="rdfs-seeAlso">ftok()</a> - Convert a pathname and a project identifier to a System V IPC key</span></li>
   </ul>
  </p>
 </div>
  

</div></div></div></body></html>